package org.example.kg_back.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.example.kg_back.PoJo.entity.MultiRadar;
import org.example.kg_back.PoJo.entity.WarnSimilarHistory;
import org.example.kg_back.mapper.MultiRadarMapper;
import org.example.kg_back.mapper.WarnSimilarHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Colonnello
 * @Gitee https://gitee.com/niuzhengchao
 * @Date 2021/3/15 10:34
 */
@Service
public class WarnSimilarHistoryService extends ServiceImpl<WarnSimilarHistoryMapper, WarnSimilarHistory> {


	@Autowired
	private MultiRadarMapper multiRadarMapper;
	/**
	 * 查询相似航班告警
	 * @return
	 */
	public List<WarnSimilarHistory> findWarnSimilarHistory(){
		List<WarnSimilarHistory> warnSimilarHistory = baseMapper.findWarnSimilarHistory();
		return warnSimilarHistory;
	}


	/**
	 * 根据扇区号查询相似航班
	 * @return
	 */
	public List<Map<String,String>>  findWarnSimilarOfATC(String sectorName){
		QueryWrapper<MultiRadar> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("section",sectorName);
		List<MultiRadar> list = multiRadarMapper.selectList(queryWrapper);

		List<Map<String,String>> result = new ArrayList<>();
		System.out.println(list);

		for(MultiRadar m1 :list){
			for(MultiRadar m2:list){
				String acid1 = m1.getAcid();
				String substring = acid1.substring(0,3);
				String acid2 = m2.getAcid();
				if(acid2.startsWith(substring)){
					if(acid1.equals(acid2)){
						break;
					}
					HashMap<String, String> res = new HashMap<>();
					res.put("gj", acid1 + "-" + acid2);
					res.put("gjSector",sectorName);
					result.add(res);
					break;
				}
			}
		}
		return result;
	}


	/**
	 * 查询航班数量
	 * @return
	 */
	public int findWarnSimilarOfATCCount(String sectorName){
		List<Map<String,String>> warnSimilarOfATC = this.findWarnSimilarOfATC(sectorName);
        return warnSimilarOfATC.size();

	}
}
